<?php

declare(strict_types=1);
/**
 * 
 */
namespace WanZ\App\User\Consumer;

use Hyperf\Context\Context;
use WanZ\App\User\Consumer\AbstractConsumer;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;

class Company extends AbstractConsumer
{
    /**
     * 获取指定公司的配置信息
     * 
     * @return array
     * @throws CommonException
     */
    public function getCompanyConfigByCompanyId(): array
    {
        $source = Context::get('minSource', '');
        if (empty($source)) {
            $source = Context::get('mini_source', 'wz');
        }

        $result = $this->sendRequest(['type' => $this->getType(), 'company_id' => $this->getCompanyId(), 'source' => $source]);

        if ($result['code'] != 1) {
            throw new CommonException(ErrorCode::THIRD_API_ERROR, 'publicError:'. $result['message']);
        }

        if (empty($result['data']) ||
            (!isset($result['data']['payConfig']) || empty($result['data']['payConfig'])) ||
            (!isset($result['data']['smsConfig']) || empty($result['data']['smsConfig']))) {
            throw new CommonException(ErrorCode::THIRD_API_ERROR, 'publicError: 获取公司配置信息失败');
        }

        $companyFullInfo = [
            'baseInfo'  => [
                'id'                    => $result['data']['id'],
                'name'                  => $result['data']['name'],
                'telephone'             => $result['data']['telephone'],
                'email'                 => $result['data']['email'],
                'businessLicenseNo'     => $result['data']['businessLicenseNo'],
                'dbName'                => $result['data']['dbName'],
                'qrcode'                => $result['data']['qrcode'],
                'smsSign'               => $result['data']['smsSign'] ?? '',
                'chargeSafeguardFee'    => $result['data']['chargeSafeguardFee'],
                'isOpenChargeSafeguard' => $result['data']['isOpenChargeSafeguard'],
                'rate'                  => $result['data']['rate'],
                'showAd'                => $result['data']['showAd'] ?? '',
                'renewFee'              => $result['data']['renewFee'],
                'createdAt'             => $result['data']['createdAt'],
                'updatedAt'             => $result['data']['updatedAt'],
                'deletedAt'             => $result['data']['deletedAt'],
            ]
        ];

        return array_merge($companyFullInfo,
            ['payConfig' => $result['data']['payConfig']],
            ['smsConfig' => $result['data']['smsConfig']]
        );
    }

    /**
     * 获取全部公司列表
     * 
     * @return array
     * @throws CommonException
     */
    public function getCompanyList(): ?array
    {
        $result = $this->sendRequest(['type' => $this->getType(), 'fields' => $this->getFields()], 'list');
        
        if ($result['code'] != 1) {
            throw new CommonException(ErrorCode::THIRD_API_ERROR, 'publicError:'. $result['message']);
        }

        if (empty($result['data'])) {
            throw new CommonException(ErrorCode::THIRD_API_ERROR, 'publicError:获取公司列表信息失败');
        }

        return $result['data'];
    }

    /**
     * 请求公共服务--获取公司相关信息
     * 
     * @param array $params
     * @param string $getType
     * @return array
     */
    private function sendRequest(array $params, string $getType = 'get'): array
    {
        return $this->httpRequest()->get($this->getUrl()."/company/{$getType}", $params);
    }

    /**
     * 获取公司列表指定字段集
     * 
     * @return mixed|string
     */
    private function getFields()
    {
        return $this->data['fields'] ?? 'id,name,dbName,rate,charge_safeguard_fee';
    }   

}
